package com.qipay.sp.excutor.fuhuipay;

import com.alibaba.fastjson.JSONObject;
import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

@Component
public class FuHuiNativeExcutor extends AbstractExcutor {

    private final static String MERCHANT_NO = "merchant_no";                          //商户号
    private final static String MERCHANT_REQ_NO = "merchant_req_no";                  //请求订单编号
    private final static String ORDER_AMT = "order_amt";                              //交易金额,以元为单位。
    private final static String BG_URL = "bg_url";                                     //后台通知地址地址
    private final static String BG_SERVER_AREA = "bg_server_area";                    //必填否,大陆写CHN或者为空；境外其他
    private final static String RETURN_URL = "return_url";                            //必填否,前台通知地址
    private final static String BIZ_CODE = "10002";                                   //10001：支付宝WAP 10002：支付宝扫码

    private final static String SUBJECT = "subject";                                  //商品名称
    private final static String PAYER_IP = "payer_ip";                                //付款人IP
    private final static String IS_SUPPORT_CD = "is_support_cd";                      //必填否,是否支持信用卡支付  1，则表示支持；0：表示不支持。
    private final static String SIGN = "sign";                                        //签名

    public final static String PAY_KEY = "pay_Key";                                  //签名,交易密钥

    private final static String API_URL = "http://api.tumikeji.com/api/order/create"; //请求地址 post


    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.ALIPAY_NATIVE;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put(FuHuiNativeExcutor.MERCHANT_REQ_NO, order.getTradeNo());
        apiParams.put(FuHuiNativeExcutor.SUBJECT, order.getBody());
        apiParams.put(FuHuiNativeExcutor.ORDER_AMT, order.getActualPayAmount());

        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        String subject = getVal(params, FuHuiNativeExcutor.SUBJECT).toString();
        String merchantNo = getVal(spconfig, FuHuiNativeExcutor.MERCHANT_NO).toString();
        String merchantReqNo = getVal(params, FuHuiNativeExcutor.MERCHANT_REQ_NO).toString();
        String bgUrl = getVal(params, FuHuiNativeExcutor.BG_URL).toString();
        String payKey = getVal(spconfig, FuHuiNativeExcutor.PAY_KEY).toString();
        String bdServerArea = getVal(params, FuHuiNativeExcutor.BG_SERVER_AREA).toString();
        //String returnUrl=getVal(params, FuHuiWapExcutor.RETURN_URL).toString();//前端回调地址

        //金额转换成元
        Integer amout = (Integer) getVal(params, FuHuiNativeExcutor.ORDER_AMT);
        BigDecimal bdAmout = BigDecimal.valueOf(amout);
        BigDecimal bdYuanAmout = bdAmout.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
        String orderAmt = bdYuanAmout.toString();

        //组装请求参数
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put("merchant_no", merchantNo);
        reqParams.put("biz_code", FuHuiNativeExcutor.BIZ_CODE); // 交易类型
        reqParams.put("subject", subject); // 商品名称
        reqParams.put("order_amt", orderAmt); // 交易金额
        reqParams.put("is_support_cd", "0"); // 关闭信用卡支付，降低投诉率，提高成功率
        reqParams.put("merchant_req_no", merchantReqNo); // 请求订单编号
        reqParams.put("bg_url", bgUrl); //回调地址
        reqParams.put("bg_server_area", bdServerArea); //区域，若境外可以传HK
        //reqParams.put("return_url", returnUrl); //前端回调地址

        //生成sign
        String sign = FHSignUtils.createSign(reqParams, payKey);
        reqParams.put("sign", sign); //区域，若境外可以传HK


        FuHuiWapExcutor.HFResult hfResult = new FuHuiWapExcutor.HFResult();

        Map<String, Object> resultMap = new HashMap<>();
        try {
            String result = super.doJsonPost(FuHuiNativeExcutor.API_URL, JSONObject.toJSONString(reqParams));
            hfResult = JSONObject.parseObject(result, FuHuiWapExcutor.HFResult.class);
        } catch (Exception e) {
            hfResult.setRsp_code("11");
            hfResult.setRsp_msg("支付宝接口异常");
        }
        if (hfResult != null) {
            if (hfResult.isSuccess()) {
                resultMap.put("result_code", "0");
                resultMap.put("redirect_url", hfResult.getRsp_pay_url());
            } else {
                logger.info("支付宝当面付接口请求失败：" + hfResult.getRsp_msg());
                resultMap.put("result_code", "1");
                resultMap.put("message", hfResult.getRsp_msg());
            }
        }
        return resultMap;
    }
}

